import {useEffect,useState} from "react"

// 自定义hook要use开头使用 
// 把通用的逻辑封装起来
export const useMouseMove=(time)=>{
    const [position, setPosition] = useState({ x: 0, y: 0 })

    useEffect(() => {
        let timer=null
        // 1、绑定事件
        const moveHandle = (e) => {
            if(timer)clearTimeout(timer)
                timer=setTimeout(()=>{
                    console.log({ x: e.clientX, y: e.clientY });
                    setPosition({ x: e.clientX, y: e.clientY })
                    timer=null
                },time)
            
        }
        // 2、组件渲染完毕后为window对象添加mousemoves事件
        window.addEventListener('mousemove', moveHandle)
        return () => 
            window.removeEventListener('mousemove', moveHandle);
        
    }, [])
    return position
}

export const useCountDown=(second=5)=>{
    const [count,setCount]=useState(second)
    const [disabled,setDisabled]=useState(true)

    useEffect(()=>{
        setTimeout(()=>{
            if(count>1){
                setCount((pre)=>pre-1)
            }else{
                setDisabled(false)
            }
        },1000)
    },[count])
    return [count,disabled]
   
}